package com.iwinding.ladygg.bluetooth.impBle;

import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;

import com.iwinding.ladygg.bluetooth.ibluetooth.IBleFunction;
import com.junkchen.blelib.BleService;


/**
 * Created by 82218 on 2017/7/12.
 */

public class BleReceiver extends BroadcastReceiver {

    private Handler bleHandler;

    private final static String PAIRING_REQUEST =  "android.bluetooth.device.action.PAIRING_REQUEST";

    public BleReceiver(Handler bleHandler) {
        this.bleHandler = bleHandler;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        BluetoothDevice btDevice=null;  //创建一个蓝牙device对象
        // 从Intent中获取设备对象
        btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);

        if (intent.getAction().equals(BleService.ACTION_BLUETOOTH_DEVICE)) {
        } else if (intent.getAction().equals(BleService.ACTION_GATT_CONNECTED)) {
        } else if (intent.getAction().equals(BleService.ACTION_GATT_DISCONNECTED)) {
        } else if (intent.getAction().equals(BleService.ACTION_SCAN_FINISHED)) {
            bleHandler.sendEmptyMessage(IBleFunction.SCAN_FINSH);
        } else if (intent.getAction().equals(PAIRING_REQUEST)){
            // 从Intent中获取设备对象
            abortBroadcast();
            bleHandler.sendEmptyMessage(IBleFunction.PAIRING_REQUEST);
//            BluetoothDevice btDevice=null;  //创建一个蓝牙device对象
//            btDevice = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
//            if(btDevice.getBondState() == BluetoothDevice.BOND_NONE){
//                try {
//                    //1.确认配对
//                    ClsUtils.setPairingConfirmation(btDevice.getClass(), btDevice, true);
//                    //2.终止有序广播
//                    abortBroadcast();//如果没有将广播终止，则会出现一个一闪而过的配对框。
//                    //3.调用setPin方法进行配对...
//                    ClsUtils.setPin(btDevice.getClass(), btDevice, "123456");
//                    ClsUtils.cancelPairingUserInput(btDevice.getClass(),btDevice);
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//            bleHandler.sendEmptyMessage(IBleFunction.PAIRING_REQUEST);
        } else if(intent.getAction().equals(BluetoothDevice.ACTION_FOUND)){
//            BleDevice bleDevice = new BleDevice();
//            bleDevice.deviceName = btDevice.getName();
//            bleDevice.deviceAddress = btDevice.getAddress();
//            //信号强度。
//            if (btDevice.getBondState() != BluetoothDevice.BOND_BONDED) {
//                short rssi=intent.getShortExtra(BluetoothDevice.EXTRA_RSSI, Short.MIN_VALUE);
//                bleDevice.deviceRssi = String.valueOf(rssi);
//            }
//            bleDevice.isConnect = false;
//            bleDevice.device = btDevice;
//                if (bleService.getConnectDevice() != null) {
//                    BluetoothDevice bluetoothDevice = bleService.getConnectDevice();
//                    String connectAdr = bluetoothDevice.getAddress();
//                    if (!TextUtils.isEmpty(connectAdr) && bluetoothDevice.getAddress().equals(device.getAddress())) {
//                        bleDevice.isConnect = true;
//                    }
//                }
//            LogUtil.i("@hzy", "scanBle----------add:" + bleDevice.deviceAddress);
//            Message message = bleHandler.obtainMessage(IBleFunction.ACTION_FOUND);
//            message.obj = bleDevice;
//            bleHandler.sendMessage(message);
        }
    }

    protected static IntentFilter makeIntentFilter() {
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BleService.ACTION_BLUETOOTH_DEVICE);
        intentFilter.addAction(BleService.ACTION_GATT_CONNECTED);
        intentFilter.addAction(BleService.ACTION_GATT_DISCONNECTED);
        intentFilter.addAction(BleService.ACTION_SCAN_FINISHED);
        intentFilter.addAction(BleService.ACTION_SCAN_FINISHED);
        intentFilter.addAction(PAIRING_REQUEST);
        return intentFilter;
    }
}
